package com.example.demo.service;

//package com.example.demo.service;

import com.example.demo.entity.DemoEntity;
import com.example.demo.repository.DemoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class DemoService {
    @Autowired
    private DemoRepository demoRepository;

    public List<DemoEntity> getAll() {
        return demoRepository.findAll();
    }

    public Optional<DemoEntity> getById(Long id) {
        return demoRepository.findById(id);
    }

    public DemoEntity create(DemoEntity entity) {
        return demoRepository.save(entity);
    }

    public DemoEntity update(Long id, DemoEntity entityDetails) {
        DemoEntity entity = demoRepository.findById(id).orElseThrow();
        entity.setName(entityDetails.getName());
        entity.setDescription(entityDetails.getDescription());
        return demoRepository.save(entity);
    }

    public void delete(Long id) {
        demoRepository.deleteById(id);
    }
}

